package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"goods/models"
	"gorm.io/gorm"
)

type GoodsController struct {
	beego.Controller
}

func (c *GoodsController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	limit, _ := c.GetInt("limit", 10)
	page, _ := c.GetInt("page", 1)
	name := c.GetString("name")
	var goods []models.Good
	sql := models.DB.Offset((page - 1) * limit).Limit(limit)
	if name != "" {
		sql = sql.Where("name like ?", "%"+name+"%")
	}
	err := sql.Find(&goods).Debug().Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	var total int
	models.DB.Model(&goods).Pluck("count(*)", &total)
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: struct {
			Total int
			Goods []models.Good
		}{
			total,
			goods,
		},
	}
	c.ServeJSON()
}
func (c *GoodsController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	price, _ := c.GetFloat("price")
	kg, _ := c.GetFloat("kg")
	stock, _ := c.GetInt("stock")
	good := models.Good{
		Name:     c.GetString("name"),
		Price:    price,
		Kilogram: kg,
		Stock:    stock,
		//Model:    gorm.Model{},
	}
	err := models.DB.Create(&good).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "添加成功",
	}
	c.ServeJSON()
}
func (c *GoodsController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	id, _ := c.GetInt("id")
	var good models.Good
	err := models.DB.Debug().Where("id = ?", id).Find(&good).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	if good.ID == 0 {
		c.Data["json"] = models.Msg{
			Code: 500,
			Msg:  "删除的目标不存在或已删除",
		}
		c.ServeJSON()
		return
	}
	err = models.DB.Delete(&models.Good{}, id).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "删除成功",
	}
	c.ServeJSON()
}
func (c *GoodsController) Update() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	id, _ := c.GetInt("id")
	if c.Ctx.Input.Method() == "GET" {
		var good models.Good
		models.DB.Where("id = ?", id).Find(&good)
		c.Data["json"] = models.Msg{
			Code: 200,
			Msg:  "查询成功",
			Data: good,
		}
		c.ServeJSON()
		return
	}
	price, _ := c.GetFloat("price")
	kg, _ := c.GetFloat("kg")
	stock, _ := c.GetInt("stock")
	good := models.Good{
		Name:     c.GetString("name"),
		Price:    price,
		Kilogram: kg,
		Stock:    stock,
		Model:    gorm.Model{ID: uint(id)},
	}
	err := models.DB.Updates(&good).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Msg:  "服务器繁忙",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Msg:  "修改成功",
	}
	c.ServeJSON()
}
